<?php

/**
 * 登录、登出日志模型
 *
 * @author JiangJian <silverd@sohu.com>
 * @copyright 2011-2012 xiangle.com
 * $Id: LoginLog.php 162 2012-11-09 04:04:30Z Jun.Lu.726@gmail.com $
 * @version    2012-02-20  ::  JiangJian  ::  Create File
 */
class Model_System_LoginLog extends Model_AbstractDao
{

    protected $_dbName     = 'BingoMobi';
    protected $_tableName  = 'bg_sys_login_log';
    protected $_primaryKey = 'id';
    protected $_nameField  = '';

    /**
     * 操作类型
     *
     * @var array
     */
    private $_opTypes = array(
        1 => '登入',
        2 => '登出',
    );

    public function getOpTypes()
    {
        return $this->_opTypes;
    }

    /**
     * 获取记录条数
     *
     * @param array $whereArr
     * @return int
     */
    public function getCount($whereArr = array())
    {
        $whereSql = Com_DB::getWhereSQL($whereArr);
        return $this->_db()->fetchOne("SELECT COUNT(0) FROM `bg_sys_login_log` AS ll
            LEFT JOIN `bg_sys_admin` AS sa ON (sa.admin_id = ll.admin_id)
            WHERE {$whereSql}");
    }

    /**
     * 获取列表
     *
     * @param mixed $whereArr
     * @param int $start
     * @param int $pageSize
     * @param string $orderBy
     * @return array
     */
    public function getList($whereArr = array(), $start = 0, $pageSize = 0, $orderBy = 'id DESC')
    {
        $whereSql = Com_DB::getWhereSQL($whereArr);
        return $this->_db()->limitQuery("SELECT * FROM `bg_sys_login_log` AS ll
            LEFT JOIN `bg_sys_admin` AS sa ON (sa.admin_id = ll.admin_id)
            WHERE {$whereSql} ORDER BY {$orderBy}", $start, $pageSize);
    }

    /**
     * 添加一条日志
     *
     * @param array $authUser 当前操作人信息
     * @param int $type 操作类型 1:登入 2:登出
     * @return bool
     */
    public function add($authUser, $type = 1)
    {
        if (!isset($this->_opTypes[$type])) {
            return false;
        }

        $setArr = array(
            'admin_id'   => $authUser['admin_id'],
            'admin_name' => $authUser['admin_name'],
            'login_time' => time(),
            'login_ip'   => Helper_Client::getUserIp(),
            'type'       => $type,
        );

        return $this->_db()->insert('bg_sys_login_log', $setArr);
    }

}